﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//
using System.Web.Services;
using DDSC.OracleADO;
using System.Collections;
using System.Text;
using System.Data;

public partial class krj_tracking_defect : BasePermissionPage
{
    
    //搜尋SQL
    [WebMethod]
    public static Object GetDocument(string DISPATCH_DEPT, string RECEIVED_DEPT, string DISPATCH_DATE, string BULLETIN_DATE, PageInfo p_PageInfo)
    {
        Object results = new Object();
        try
        {
            dbAccess m_dba = new dbAccess();
            StringBuilder m_sb = new StringBuilder();
            ArrayList m_arrParam = new ArrayList();
            m_sb.Append(" select F60501 DETAIL_NO,F60502 ITEM_NO,B.DEPT_NAME DISPATCH_DEPT, F60503 DISPATCH_DATE,C.DEPT_NAME RECEIVED_DEPT,F60505 BULLETIN_DATE, F60507 SHIPPER, F60508 RECEIVER,F60510 ITEM_COUNT, F60514 UNCORRECT_ITEM_COUNT, F60515 BORKEN_ITEM_COUNT from as400_ZDF605 A left join MIS_DEPT_AP B on A.F60506 = B.DEPT_ID left join MIS_DEPT_AP C on A.F60517 = C.DEPT_ID  ");
            m_sb.Append(" where 1 = 1 ");
            
            if (DISPATCH_DATE != "")
            {
                m_sb.Append(" and F60503 >=:DISPATCH_DATE ");
                m_arrParam.Add(m_dba.GetOracleParam(":DISPATCH_DATE", DISPATCH_DATE));
            }
            if (BULLETIN_DATE != "")
            {
                m_sb.Append(" and F60505 >=:BULLETIN_DATE ");
                m_arrParam.Add(m_dba.GetOracleParam(":BULLETIN_DATE", BULLETIN_DATE));
            }
            if (DISPATCH_DEPT != "")
            {
                m_sb.Append(" and F60506 =:DISPATCH_DEPT ");
                m_arrParam.Add(m_dba.GetOracleParam(":DISPATCH_DEPT", DISPATCH_DEPT));
            }
            if (RECEIVED_DEPT != "")
            {
                m_sb.Append(" and F60517 =:RECEIVED_DEPT ");
                m_arrParam.Add(m_dba.GetOracleParam(":RECEIVED_DEPT", RECEIVED_DEPT));
            }
            results = ADOGridHandler(m_sb, m_arrParam, p_PageInfo);
            //20130628**********************************************
            //記錄最後查詢的條件
            krj_tracking_defect sa = new krj_tracking_defect();
            sa.savecondition( DISPATCH_DEPT,  RECEIVED_DEPT,  DISPATCH_DATE,  BULLETIN_DATE);
        }
        catch (Exception ex)
        {
            //results = ex.Message;
            BasePermissionPage BP = new BasePermissionPage();
            results = BP.BP_Message;
            BP.ERROR_DBSAVE("krj_tracking_defect.aspx.cs", "GetDocument", ex.Message, "", "c#", "0", ex.StackTrace);
        }
        return (results);

    }
    

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            DataSet ds = null;
            ds = DBINT();
            if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() == "000")
            {
                //DropDownList資料bind入
                ddlDISPATCH_DEPT.DataTextField = "DEPT_NAME";
                ddlDISPATCH_DEPT.DataValueField = "DEPT_ID";
                ddlDISPATCH_DEPT.DataSource = ds.Tables["DEPT"];
                ddlDISPATCH_DEPT.DataBind();


                ddlRECEIVE_DEPT.DataTextField = "DEPT_NAME";
                ddlRECEIVE_DEPT.DataValueField = "DEPT_ID";
                ddlRECEIVE_DEPT.DataSource = ds.Tables["DEPT"];
                ddlRECEIVE_DEPT.DataBind();
            }
            //紀錄頁次
            var m_PageIndex = "1";
            //判斷如果是維護頁導回來就填回查詢條件
            if (Page.PreviousPage != null)
            {

                //執行搜尋功能
                ClientScript.RegisterClientScriptBlock(this.GetType(), "btnSearch_Click", "<script language='javascript'>$(document).ready(function () { btnSearch_Click('" + m_PageIndex + "'); }) </script>");
            }
        }
        catch (Exception ex)
        {
            lbl_message.Text = BP_Message;
            ERROR_DBSAVE("krj_tracking_defect.aspx.cs", "GetDocument", ex.Message, "", "c#", "0", ex.StackTrace);
        }
    }
    
    protected DataSet DBINT()
    {
        DataSet ds = null;
        try
        {
            dbAccess m_dba = new dbAccess();
            StringBuilder m_sb = new StringBuilder();
            ArrayList m_arrParam = new ArrayList();

            m_sb.Append(" select A.bi4101 DEPT_ID ,A.bi4101 || '-' || B.DEPT_NAME DEPT_NAME ");
            m_sb.Append(" from as400_bif41 A left join MIS_DEPT_AP  B on A.BI4101 = B.DEPT_ID ");
            m_sb.Append(" where BI4121 = '1' and BI4122 = '2' ");
            m_sb.Append(" order by A.bi4101 ");

            ds = m_dba.runExecuteQuery("DEPT", m_sb.ToString(), m_arrParam);
            //if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() != "000")
            //{
            //    lbl_message.Text = "as400_bif41: 資料讀取錯誤！" + ds.Tables[0].Rows[0]["LogMessage"];
            //}

        }
        catch (Exception ex)
        {
            lbl_message.Text = BP_Message;
            ERROR_DBSAVE("krj_tracking_defect.aspx.cs", "DBINT", ex.Message, "", "c#", "0", ex.StackTrace);
        }
        return ds;
    }
    protected void btn_exist_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx");
    }
    protected void savecondition(string DISPATCH_DEPT, string RECEIVED_DEPT, string DISPATCH_DATE, string BULLETIN_DATE)
    {
        try
        {
            DropDownList ddl = new DropDownList();
            TextBox tx = new TextBox();

            var controlItem = new List<ControlItem>() 
            {
              new ControlItem("ddlDISPATCH_DEPT", DISPATCH_DEPT, ddl.GetType()),
              new ControlItem("ddlRECEIVE_DEPT", RECEIVED_DEPT, ddl.GetType()),
              new ControlItem("txtDISPATCH_DATE", DISPATCH_DATE, tx.GetType()) ,
              new ControlItem("txtBULLETIN_DATE", BULLETIN_DATE, tx.GetType())      
            };
            ConditionRecordUtil.RecordSearchConditionData(controlItem);
        }
        catch (Exception ex)
        {
            ERROR_DBSAVE("krj_tracking_defect.aspx.cs", "savecondition", ex.Message, "", "c#", "0", ex.StackTrace);
        }
    }
}